using System.Linq;
using System.Collections.Generic;
using Managistics.Model;
using NHibernate.Criterion;
using NHibernate;
using System;
using Managistics.Model.ValueObject;
using Managistics.Utility;

namespace Managistics.Core.Repositories.Impl
{
    public class PurchaseOrderDetailRepository : RepositoryBase<PurchaseOrderDetail, double>, IPurchaseOrderDetailRepository
    {
        /// <summary>
        /// Select PurchaseOrderDetail
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<PurchaseOrderDetail> GetPurchaseOrderDetail(PurchaseOrderDetailVO param)
        {
            var query = this.Session.QueryOver<PurchaseOrderDetail>();

            if (!String.IsNullOrEmpty(param.Orderid))
            {
                Good good = null;
                Unit unitItem = null;

                query.Where(x => x.Orderid == param.Orderid);
                query.Left.JoinQueryOver(x => x.Good, () => good);
                query.Left.JoinQueryOver(x => x.Good.UnitItem, () => unitItem);
                query.Left.JoinQueryOver(p => p.PurchaseOrder);
            }

            //pagination===========================
            List<PurchaseOrderDetail> result = this.QueryPagination(query, param.page_number, param.page_size);
            param.total_row = this.RowCount;
            param.total_page = this.TotalPage+1;
            return result;                        
        }

        /// <summary>
        /// Select PurchaseOrderDetails by OrderId
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public List<PurchaseOrderDetail> GetPurchaseOrderDetailByOrderId(string orderId)
        {
            var query = this.Session.QueryOver<PurchaseOrderDetail>();
            query.Where(x => x.Orderid == orderId);

            return query.List<PurchaseOrderDetail>().ToList<PurchaseOrderDetail>();
        }

    }
}